package com.open.party.cloud.view.home.jiCengDangJian.org

import android.os.Bundle
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import com.sinothk.android.utils.inters.OnSuperListener
import com.open.party.cloud.R
import com.open.party.cloud.model.PartyMemberBean
import com.open.party.cloud.model.PartyMemberVo
import com.open.party.cloud.view.home.jiCengDangJian.org.adapter.PartyMemberListAdapter
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class PartyMemberListActivity : AppEventBusRecycleViewTitleBaseActivity<PartyMemberVo>(), OnSuperListener<PartyMemberBean> {
    var viewModel: BizViewModel? = null
    var orgId = ""
    var orgName = ""

    private var adapter: PartyMemberListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        orgId = intent.getStringExtra("orgId") as String
        orgName = intent.getStringExtra("orgName") as String
        setViewTitle(orgName)

        if (XUtils.string().isEmpty(orgId)) {
            finish()
        }

        viewModel = BizViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = PartyMemberListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.setRetryListener {
            refresh()
        }
        refresh()
    }

    private fun refresh() {
        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<PartyMemberVo>) {
        val pageReq = PageReq<PartyMemberVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize

        val vo = PartyMemberVo()
        vo.orgId = orgId
        pageReq.data = vo
        viewModel!!.getPartyMemberListData(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<PartyMemberBean>>) {
        if (result.eventType != "getPartyMemberListData") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    private fun setPageData(appPageData: AppPageData<PartyMemberBean>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    override fun onClick(position: Int, itemData: PartyMemberBean, flag: String?) {
        if (XUtils.string().isNotEmpty(itemData.phone)) {
            XUtils.phone().callDial(this@PartyMemberListActivity, itemData.phone)
        }
    }
}